import requests
from bs4 import BeautifulSoup
import time
import os
import random
def AA(rote,name_sing=None):
    pro = ["113.195.210.222:61202","60.162.61.73:61202","113.122.45.196:61234"]
    url = 'http://5sing.kugou.com/jingcheng6sq/yc/1.html'
    header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36'}
    b = []
    while True:
        html = requests.get(url, headers=header,proxies={"HTTPS": random.choice(pro)})
        soup = BeautifulSoup(html.text, "html5lib")
        ti = 1
        for each in soup.find_all('div', class_='song_name'):
            address = each.a["href"]
            name = each.a.text
            print("----------")
            print(name,address)
            if name_sing is not None and name == name_sing:
                print("停")
                return b
            result = BB(address,rote,type="yuan")
            result["地址"] = address
            result["歌名"] = name
            b.append(result)
            ti += 1
            if ti == 5:
                print("休息10秒")
                time.sleep(10)
                ti =1
            time.sleep(1)
        m = soup.select(".page_next")
        if m == []:
            break
        else:
            her=m[0]["href"]
            url = 'http://5sing.kugou.com' + her
    return b

def BB(url,rote,type):
    pro = ["113.195.210.222:61202", "60.162.61.73:61202", "113.122.45.196:61234"]
    header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36'}
    html = requests.get(url, headers=header,proxies={"HTTPS": random.choice(pro)})
    soup = BeautifulSoup(html.text, "html5lib")
    b = {}
    for each in soup.find_all('ul', class_='lt mb15'):
        for i in each.find_all("li"):
            if i.a is not None:
                b[i.em.text] = i.a.text
            else:
                b[i.em.text] = i.text
    for ebch in soup.find_all("div", class_="lrc_info_clip lrc-tab-content"):
        ge = str(ebch.div).replace(" ","")
        b["歌词"] = ge
    for ecch in soup.find_all("div",class_="lrc_info_clip inspiration-tab-content"):
        me = ecch.div.img
        print('me',me)
        try:
            result = CC(me["src"], rote,type)
            b["图片地址"] = result
        except:
            b["图片地址"] = None
    return b

def CC(url, rote,type):
    pro = ["125.118.246.23:808", "122.114.31.177:808", "125.118.144.48:808"]
    header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36'}
    html1 = requests.get(url, headers=header,proxies={"HTTP": random.choice(pro)})
    a = rote + "/static/"
    if type == "fan":
        c = "images/moves/fan/" + str(int(time.time())) + ".jpg"
    elif type == "yuan":
        c = "images/moves/yuan/" + str(int(time.time())) +".jpg"
    b = ''.join([a,c])
    with open(b, "wb") as f:
        f.write(html1.content)
        f.close()
    return c



def DD(rote,name_sing=None):
    pro = ["113.195.210.222:61202", "60.162.61.73:61202", "113.122.45.196:61234"]
    url = 'http://5sing.kugou.com/jingcheng6sq/fc/1.html'
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36'}
    b = []
    while True:
        html = requests.get(url, headers=header, proxies={"HTTPS": random.choice(pro)})
        soup = BeautifulSoup(html.text, "html5lib")
        ti = 1
        for each in soup.find_all('div', class_='song_name'):
            address = each.a["href"]
            name = each.a.text
            print("----------")
            print(name, address)
            if name_sing is not None and name == name_sing:
                print("停")
                return b
            result = BB(address, rote,type="fan")
            result["地址"] = address
            result["歌名"] = name
            b.append(result)
            ti += 1
            if ti == 5:
                print("休息10秒")
                time.sleep(10)
                ti = 1
            time.sleep(1)
        m = soup.select(".page_next")
        if m == []:
            break
        else:
            her = m[0]["href"]
            url = 'http://5sing.kugou.com' + her
    return b



if __name__ == "__main__":
    rote = os.path.dirname(__file__)
    AA(rote)